home *** CD-ROM | disk | FTP | other *** search
- /*
- Module : DATETIME.H
- Purpose: Defines the interface to a number of Date and Date/Time classes
- Created: PJN / DATE/1 / 05-05-1995
- History: None
-
- Copyright (c) 1995 by PJ Naughter.
- All rights reserved.
-
- */
-
- #ifndef __DATE_H__
-
-
- ////////////////////////////////// Macros /////////////////////////////////////
-
- #define __DATE_H__
-
-
-
- ////////////////////////////////// Includes ///////////////////////////////////
-
- #include <afx.h>
- #include <iostream.h>
-
-
-
- ////////////////////////////// Date Enums & Structs ///////////////////////////
-
- //flags used to control how the CLTimeSpan::Format(DWORD dwFlags),
- //CLTimeOfDay::Format(DWORD dwFlags) and CLDate::Format(DWORD dwFlags) behave
- const DWORD DTF_NOSECOND = 0x0001; //do not include the second field
- const DWORD DTF_NOTIMEFRAME = 0x0002; //Do not include the TimeFrame field
-
- enum CalendarType { JULIAN, GREGORIAN };
-
-
- struct DateS
- {
- LONG lYear;
- WORD wMonth;
- WORD wDay;
- WORD wWday;
- WORD wYday;
- CalendarType ct;
- };
-
-
- enum TimeFrame {UCT=0,
- ET,
- LOCAL};
-
-
- struct DateLS
- {
- LONG lYear;
- WORD wMonth;
- WORD wDay;
- WORD wWday;
- WORD wYday;
- WORD wHour;
- WORD wMinute;
- WORD wSecond;
- CalendarType ct;
- TimeFrame tf;
- };
-
-
- /////////////////////////////// Free sub-programs (aka functions) /////////////
-
- //Archive operators for 64 bit integers
-
- //This need to be provided as currently MFC does not support archiving of
- //64 bit integers. When this become part of MFC, the will be removed
- //from here
- inline CArchive& AFX_EXT_API operator<<(CArchive& ar, __int64 nInt64)
- {
- ar.Write(&nInt64, sizeof(__int64));
- return ar;
- };
-
- inline CArchive& AFX_EXT_API operator>>(CArchive& ar, __int64 nInt64)
- {
- ar.Read(&nInt64, sizeof(__int64));
- return ar;
- };
-
- inline CArchive& AFX_EXT_API operator<<(CArchive& ar, unsigned __int64 nInt64)
- {
- ar.Write(&nInt64, sizeof(unsigned __int64));
- return ar;
- };
-
- inline CArchive& AFX_EXT_API operator>>(CArchive& ar, unsigned __int64 nInt64)
- {
- ar.Read(&nInt64, sizeof(unsigned __int64));
- return ar;
- };
-
-
- void AFX_EXT_API InitDTime(); //Should be called when initialising DTime and in
- //response to WM_SETTINGCHANGE messages
-
-
-
-
- /////////////////////////////// Classes ///////////////////////////////////////
-
- // forward declarations
- class CLDate;
- class CLTimeOfDay;
-
-
-
-
- // A Date class with a granularity of 1 Day
-
- class AFX_EXT_CLASS CDate : public CObject
- {
- DECLARE_SERIAL(CDate)
-
- public:
- friend class CLDate;
-
- enum MONTH { JANUARY=1,
- FEBRUARY,
- MARCH,
- APRIL,
- MAY,
- JUNE,
- JULY,
- AUGUST,
- SEPTEMBER,
- OCTOBER,
- NOVEMBER,
- DECEMBER};
-
- enum DayOfWeek { SUNDAY=1,
- MONDAY,
- TUESDAY,
- WEDNESDAY,
- THURSDAY,
- FRIDAY,
- SATURDAY};
-
- enum DateEpoch { EPOCH_JD,
- EPOCH_MJD,
- EPOCH_1900,
- EPOCH_1950,
- EPOCH_CTIME,
- EPOCH_2000,
- EPOCH_GREG};
-
-
- //constructors
- CDate();
- CDate(LONG Year, WORD Month, WORD Day);
- CDate(const SYSTEMTIME& st);
- CDate(LONG Year, WORD Month, WORD WeekOfMonth, WORD DayOfWeek);
- CDate(LONG Days, DateEpoch e);
- CDate(const CDate& d);
- CDate(const CTime& ctime);
- CDate(const COleDateTime& oleTime);
-
-
- //Set operators
- CDate& Set();
- CDate& Set(LONG Year, WORD Month, WORD Day);
- CDate& Set(const SYSTEMTIME& st);
- CDate& Set(LONG Year, WORD Month, WORD WeekOfMonth, WORD DayOfWeek);
- CDate& Set(LONG Days, DateEpoch e);
- CDate& Set(const CTime& ctime);
- CDate& Set(const COleDateTime& oleTime);
-
-
- //statics constructors
- static CDate CurrentDate();
- static CDate FirstCurrentMonth();
- static CDate LastCurrentMonth();
- static CDate FirstCurrentYear();
- static CDate LastCurrentYear();
- static CDate JDEpoch();
- static CDate MJDEpoch();
- static CDate Epoch1900();
- static CDate Epoch1950();
- static CDate EpochCTime();
- static CDate Epoch2000();
- static CDate GregorianEpoch();
-
-
- //static operations
- static BOOL InitCalendarSettings();
- static void GetDefaultFormat();
- static BOOL IsLeap(LONG Year);
- static WORD DaysInYear(LONG Year);
- static WORD DaysInMonth(WORD Month, BOOL IsLeap);
- static WORD DaysSinceJan1(WORD Month, WORD Day, BOOL IsLeap);
- static WORD DaysSinceJan0(WORD Month, WORD Day, BOOL IsLeap);
- static WORD CurrentMonth();
- static WORD CurrentDay();
- static LONG CurrentYear();
- static WORD CurrentDayOfWeek();
- static WORD GetBeginingDayOfWeek();
- static BOOL SetEndJulianCalendar(LONG Year, WORD Month, WORD Day);
- static BOOL SetBeginGregorianCalendar(LONG Year, WORD Month, WORD Day);
- static void GetEndJulianCalendar(LONG& Year, WORD& Month, WORD& Day);
- static void GetBeginGregorianCalendar(LONG& Year, WORD& Month, WORD& Day);
- static BOOL InGregorianCalendar(LONG Year, WORD Month, WORD Day);
- static BOOL InJulianCalendar(LONG Year, WORD Month, WORD Day);
- #ifdef _DEBUG
- static BOOL SetDoConstructorAsserts(BOOL bDoAsserts);
- #endif
- static CString GetFullStringDayOfWeek(WORD DayOfWeek);
- static CString GetAbrStringDayOfWeek(WORD DayOfWeek);
- static CString GetFullStringMonth(WORD Month);
- static CString GetAbrStringMonth(WORD Month);
- static LONG EndJulianYear();
- static WORD EndJulianMonth();
- static WORD EndJulianDay();
- static LONG BeginGregorianYear();
- static WORD BeginGregorianMonth();
- static WORD BeginGregorianDay();
-
-
-
- //static Holiday constructors
- static CDate NewYearsDay(LONG Year=CDate::CurrentYear());
- static CDate ValentinesDay(LONG Year=CDate::CurrentYear());
- static CDate AshWednesday(LONG Year=CDate::CurrentYear());
- static CDate StPatricksDay(LONG Year=CDate::CurrentYear());
- static CDate GoodFriday(LONG Year=CDate::CurrentYear());
- static CDate EasterSunday(LONG Year=CDate::CurrentYear());
- static CDate CanadaDay(LONG Year=CDate::CurrentYear());
- static CDate IndependenceDay(LONG Year=CDate::CurrentYear());
- static CDate BastilleDay(LONG Year=CDate::CurrentYear());
- static CDate ChristmasDay(LONG Year=CDate::CurrentYear());
-
-
- //Operations
- WORD DaysSinceJan1() const;
- WORD DaysSinceJan0() const;
- DateS GetDate() const;
- WORD GetDay() const;
- WORD GetMonth() const;
- LONG GetYear() const;
- LONG GetCEBCEYear(BOOL& IsCE) const;
- WORD Get2DigitYear() const;
- LONG Since1900Epoch() const;
- LONG Since1950Epoch() const;
- LONG SinceCTimeEpoch() const;
- LONG Since2000Epoch() const;
- LONG GDN() const;
- LONG GetValue() const;
- LONG JD() const;
- BOOL IsLeap() const;
- WORD DaysInYear() const;
- WORD DaysInMonth() const;
- void AddYear(int Years=1);
- void AddWeek(int Weeks=1);
- void AddMonth(int Months=1);
- WORD GetDayOfWeek() const;
- CString GetStringCEBCEYear() const;
- CString GetFullStringDayOfWeek() const;
- CString GetAbrStringDayOfWeek() const;
- CString GetFullStringMonth() const;
- CString GetAbrStringMonth() const;
- SYSTEMTIME GetSYSTEMTIME() const;
- tm GetTM() const;
- WORD GetWeekOfYear() const;
- WORD GetWeekOfMonth() const;
- BOOL IsValid() const;
- LONG Collate() const;
- BOOL InGregorianCalendar() const;
- BOOL InJulianCalendar() const;
-
-
- //Creation of other CDate's from this instance
- CDate FirstThisMonth() const;
- CDate LastThisMonth() const;
- CDate FirstThisYear() const;
- CDate LastThisYear() const;
-
-
- //Overloaded Arithmetic Operators
- CDate& operator=(const CDate& d);
- CDate operator+(LONG Days) const;
- LONG operator-(const CDate& d) const;
- CDate operator-(LONG Days) const;
- CDate& operator+=(LONG Days);
- CDate& operator-=(LONG Days);
- CDate& operator++();
- CDate& operator--();
-
-
- //Overloaded Equality operators
- BOOL operator==(const CDate& d) const;
- BOOL operator>(const CDate& d) const;
- BOOL operator>=(const CDate& d) const;
- BOOL operator<(const CDate& d) const;
- BOOL operator<=(const CDate& d) const;
- BOOL operator!=(const CDate& d) const;
-
-
- //Diagnostics / Debug
- #ifdef _DEBUG
- virtual void AssertValid() const;
- virtual void Dump(CDumpContext& dc) const;
- #endif
-
-
- //Display
- CString Format(const CString& sFormat) const; //Format using a format specifier string
- CString Format() const; //Format using the current NLSAPI settings
-
-
- //Serialization
- virtual void Serialize(CArchive& ar);
-
-
- //streaming
- friend AFX_EXT_API ostream& operator<<(ostream& os, const CDate& Date);
- friend AFX_EXT_API CArchive& operator<<(CArchive& ar, CDate& Date);
- friend AFX_EXT_API CArchive& operator>>(CArchive& ar, CDate& Date);
-
-
- protected:
- LONG m_lDays; //Count of days since 15 October 1582 (Gregorian Calendrical System or n.s)
- BOOL m_bInGregCalendar; //Is this date in the Gregorian Calendar
- BOOL m_bValid; //Is this a valid instance
-
- static BOOL sm_bDoAsserts; //Do constructor asserts
-
-
- //Following values are managed by CDate::InitCalendarSettings and CDate::GetDefaultFormat
- static LONG sm_lEndJulianYear; //The Date when the Julian Calendar Finished
- static WORD sm_wEndJulianMonth;
- static WORD sm_wEndJulianDay;
- static LONG sm_lBeginGregYear; //The Date when the Gregorian Calendar Started
- static WORD sm_wBeginGregMonth;
- static WORD sm_wBeginGregDay;
- static CString sm_sDefaultFormat; //Format string to use in Format()
- };
-
-
-
-
- // A Time span class with a granularity of 1 second which
- // is used in conjunction with the time class CLDate
-
- class AFX_EXT_CLASS CLTimeSpan : public CObject
- {
- public:
- friend class CLDate;
- friend class CLTimeOfDay;
-
- DECLARE_SERIAL(CLTimeSpan)
-
- //constructors
- CLTimeSpan();
- CLTimeSpan(LONG Day, WORD Hour, WORD Minute, WORD Second);
- CLTimeSpan(const CLTimeSpan& lts);
- CLTimeSpan(const CTimeSpan& ts);
- CLTimeSpan(const CLTimeOfDay& tod);
- CLTimeSpan(const COleDateTimeSpan& oleTimeSpan);
- CLTimeSpan(const __int64& Seconds);
-
-
- //Set operators
- CLTimeSpan& Set();
- CLTimeSpan& Set(LONG Day, WORD Hour, WORD Minute, WORD Second);
- CLTimeSpan& Set(const CTimeSpan& ts);
- CLTimeSpan& Set(const CLTimeOfDay& tod);
- CLTimeSpan& Set(const COleDateTimeSpan& oleTimeSpan);
- CLTimeSpan& Set(const __int64& Seconds);
-
-
- //statics constructors
- static CLTimeSpan OneCivilYear();
- static CLTimeSpan OneDay();
- static CLTimeSpan OneHour();
- static CLTimeSpan OneMinute();
- static CLTimeSpan OneSecond();
-
- //static operations
- static void GetDefaultFormat();
- #ifdef _DEBUG
- static BOOL SetDoConstructorAsserts(BOOL bDoAsserts);
- #endif
-
-
- //Operations
- LONG GetTotalDays() const;
- WORD GetHours() const;
- WORD GetMinutes() const;
- WORD GetSeconds() const;
- BOOL IsValid() const;
- CLTimeSpan& Negate();
- BOOL IsPositiveSpan() const;
- double SecondsAsDouble() const;
-
-
- //Overloaded Arithmetic Operators
- CLTimeSpan& operator=(const CLTimeSpan& TimeSpan);
- CLTimeSpan operator+(const CLTimeSpan& TimeSpan) const;
- CLTimeSpan operator-(const CLTimeSpan& TimeSpan) const;
- CLTimeSpan& operator+=(CLTimeSpan& TimeSpan);
- CLTimeSpan& operator-=(CLTimeSpan& TimeSpan);
- friend AFX_EXT_API CLTimeSpan operator-(const CLTimeSpan& TimeSpan);
- CLTimeSpan operator*(WORD Multiplier) const;
- friend AFX_EXT_API CLTimeSpan operator*(WORD Multiplier, const CLTimeSpan& TimeSpan);
- CLTimeSpan operator/(WORD divisor) const;
- CLTimeSpan& operator*=(WORD Multiplier);
- CLTimeSpan& operator/=(WORD Divisor);
-
-
-
- //Overloaded Equality operators
- BOOL operator==(const CLTimeSpan& TimeSpan) const;
- BOOL operator>(const CLTimeSpan& TimeSpan) const;
- BOOL operator>=(const CLTimeSpan& TimeSpan) const;
- BOOL operator<(const CLTimeSpan& TimeSpan) const;
- BOOL operator<=(const CLTimeSpan& TimeSpan) const;
- BOOL operator!=(const CLTimeSpan& TimeSpan) const;
-
-
- //Diagnostics / Debug
- #ifdef _DEBUG
- virtual void AssertValid() const;
- virtual void Dump(CDumpContext& dc) const;
- #endif
-
-
- //Display
- CString Format(const CString& sFormat) const; //Format using a format specifier string
- CString Format(DWORD dwFlags = 0) const; //Format using the current NLSAPI settings
-
-
- //Serialization
- virtual void Serialize(CArchive& ar);
-
-
- //streaming
- friend AFX_EXT_API ostream& operator<<(ostream& os, const CLTimeSpan& TimeSpan);
- friend AFX_EXT_API CArchive& operator<<(CArchive& ar, CLTimeSpan& TimeSpan);
- friend AFX_EXT_API CArchive& operator>>(CArchive& ar, CLTimeSpan& TimeSpan);
-
-
- protected:
- __int64 m_nSeconds; //Actual instance data
- BOOL m_bValid; //Is this a valid instance
-
- static BOOL sm_bDoAsserts; //Do Constructor asserts
- static CString sm_sDefaultFormat; //Format string to use in Format()
- static CString sm_sDefaultFormatNOS; //Format string to use in Format() with DTF_NOSECOND
- };
-
-
-
-
- // A Time of Day class with a granularity of 1 second which
- // is used in conjunction with the time class CDate in the CLDate class
-
- class AFX_EXT_CLASS CLTimeOfDay : public CObject
- {
- public:
- friend class CLDate;
-
- DECLARE_SERIAL(CLTimeOfDay)
-
- //constructors
- CLTimeOfDay();
- CLTimeOfDay(WORD Hour, WORD Minute, WORD Second);
- CLTimeOfDay(const SYSTEMTIME& st);
- CLTimeOfDay(const CLTimeOfDay& ltod);
- CLTimeOfDay(DWORD TotalSeconds);
-
-
- //Set operators
- CLTimeOfDay& Set();
- CLTimeOfDay& Set(WORD Hour, WORD Minute, WORD Second);
- CLTimeOfDay& Set(const SYSTEMTIME& st);
- CLTimeOfDay& Set(DWORD TotalSeconds);
-
-
- //statics constructors
- static CLTimeOfDay CurrentTimeOfDay(TimeFrame tf);
- static CLTimeOfDay Midnight();
- static CLTimeOfDay Midday();
-
-
- //static operations
- static void GetDefaultFormat();
- #ifdef _DEBUG
- static BOOL SetDoConstructorAsserts(BOOL bDoAsserts);
- #endif
- static CString GetAMString();
- static CString GetPMString();
-
-
- //Operations
- WORD GetHour() const;
- WORD GetMinute() const;
- WORD GetSecond() const;
- DWORD GetTotalSeconds() const;
- WORD GetAMPMHour() const;
- CString GetAMPMString() const;
- BOOL IsValid() const;
- DWORD Collate() const;
- SYSTEMTIME GetSYSTEMTIME() const;
- tm GetTM() const;
-
-
- //Overloaded Arithmetic Operators
- CLTimeOfDay& operator=(const CLTimeOfDay& Tod);
- CLTimeOfDay operator+(const CLTimeSpan& TimeSpan) const;
- CLTimeOfDay operator-(const CLTimeSpan& TimeSpan) const;
- CLTimeOfDay& operator+=(CLTimeSpan& TimeSpan);
- CLTimeOfDay& operator-=(CLTimeSpan& TimeSpan);
-
-
- //Overloaded Equality operators
- BOOL operator==(const CLTimeOfDay& Tod) const;
- BOOL operator>(const CLTimeOfDay& Tod) const;
- BOOL operator>=(const CLTimeOfDay& Tod) const;
- BOOL operator<(const CLTimeOfDay& Tod) const;
- BOOL operator<=(const CLTimeOfDay& Tod) const;
- BOOL operator!=(const CLTimeOfDay& Tod) const;
-
-
- //Diagnostics / Debug
- #ifdef _DEBUG
- virtual void AssertValid() const;
- virtual void Dump(CDumpContext& dc) const;
- #endif
-
-
- //Display
- CString Format(const CString& sFormat) const; //Format using a format specifier string
- CString Format(DWORD dwFlags = 0) const; //Format using the current NLSAPI settings
-
-
- //Serialization
- virtual void Serialize(CArchive& ar);
-
-
- //streaming
- friend AFX_EXT_API ostream& operator<<(ostream& os, const CLTimeOfDay& Tod);
- friend AFX_EXT_API CArchive& operator<<(CArchive& ar, CLTimeOfDay& Tod);
- friend AFX_EXT_API CArchive& operator>>(CArchive& ar, CLTimeOfDay& Tod);
-
-
- protected:
- DWORD m_dwTotalSeconds; //Actual instance data
- BOOL m_bValid; //Is this a valid instance
-
- static BOOL sm_bDoAsserts; //Do Constructor asserts
- static CString sm_sDefaultFormat; //Format string to use in Format()
- static CString sm_sDefaultFormatNOS; //Format string to use in Format() with DTF_NOSECOND
- };
-
-
-
-
- // A Date class with a granularity of 1 second
-
- class AFX_EXT_CLASS CLDate : public CObject
- {
- public:
- DECLARE_SERIAL(CLDate)
-
- //constructors
- CLDate();
- CLDate(LONG Year, WORD Month, WORD Day, WORD Hour, WORD Minute, WORD Second, TimeFrame tf);
- CLDate(const SYSTEMTIME& st, TimeFrame tf, BOOL bUseDayOfWeek=FALSE);
- CLDate(LONG Year, WORD Month, WORD WeekOfMonth, WORD DayOfWeek, WORD Hour, WORD Minute, WORD Second, TimeFrame tf);
- CLDate(LONG Days, CDate::DateEpoch e, WORD Hour, WORD Minute, WORD Second, TimeFrame tf);
- CLDate(const CLDate& ld);
- CLDate(const CTime& ct);
- CLDate(const CDate& Date, const CLTimeOfDay& Tod, TimeFrame tf);
- CLDate(const COleDateTime& oleTime, TimeFrame tf);
-
-
- //Set operators
- CLDate& Set();
- CLDate& Set(LONG Year, WORD Month, WORD Day, WORD Hour, WORD Minute, WORD Second, TimeFrame tf);
- CLDate& Set(const SYSTEMTIME& st, TimeFrame tf, BOOL bUseDayOfWeek=FALSE);
- CLDate& Set(LONG Year, WORD Month, WORD WeekOfMonth, WORD DayOfWeek, WORD Hour, WORD Minute, WORD Second, TimeFrame tf);
- CLDate& Set(LONG Days, CDate::DateEpoch e, WORD Hour, WORD Minute, WORD Second, TimeFrame tf);
- CLDate& Set(const CTime& ct);
- CLDate& Set(const CDate& Date, const CLTimeOfDay& Tod, TimeFrame tf);
- CLDate& Set(const COleDateTime& oleTime, TimeFrame tf);
-
-
- //static constructors
- static CLDate CurrentTime(TimeFrame tf);
-
-
- //static operations
- static CLTimeSpan DeltaT(CLDate& ld);
- static BOOL CurrentlyInDST();
- static CLTimeSpan DaylightBias();
- static CLTimeSpan TimezoneBias();
- static CString DaylightName();
- static CString StandardName();
- #ifdef _DEBUG
- static BOOL SetDoConstructorAsserts(BOOL bDoAsserts);
- #endif
-
-
- //Operations
- CDate GetCDate() const;
- CLTimeOfDay GetCLTimeOfDay() const;
- DateLS GetDate() const;
- SYSTEMTIME GetSYSTEMTIME() const;
- tm GetTM();
- void AddYear(int Years=1);
- void AddWeek(int Weeks=1);
- void AddMonth(int Months=1);
- BOOL IsValid() const;
- CLTimeSpan DeltaT();
- TimeFrame SetTimeFrame(TimeFrame tf);
- TimeFrame GetTimeFrame() const;
- BOOL IsDST();
- CString GetStringTimeFrame() const;
-
-
- //Overloaded Arithmetic Operators
- CLDate& operator=(const CLDate& ld);
- CLDate operator+(const CLTimeSpan& TimeSpan);
- CLTimeSpan operator-(CLDate& ld);
- CLDate operator-(const CLTimeSpan& TimeSpan);
- CLDate& operator+=(const CLTimeSpan& TimeSpan);
- CLDate& operator-=(const CLTimeSpan& TimeSpan);
- CLDate& operator++();
- CLDate& operator--();
-
-
- //Overloaded Equality operators
- BOOL operator==(CLDate& ld);
- BOOL operator>(CLDate& ld);
- BOOL operator>=(CLDate& ld);
- BOOL operator<(CLDate& ld);
- BOOL operator<=(CLDate& ld);
- BOOL operator!=(CLDate& ld);
-
-
- //Diagnostics / Debug
- #ifdef _DEBUG
- virtual void AssertValid() const;
- virtual void Dump(CDumpContext& dc) const;
- #endif
-
-
- //Display
- CString Format(const CString& sFormat) const; //Format using a format specifier string
- CString Format(DWORD dwFlags = 0) const; //Format using the current NLSAPI settings
-
-
- //Serialization
- virtual void Serialize(CArchive& ar);
-
-
- //streaming
- friend AFX_EXT_API ostream& operator<<(ostream& os, const CLDate& LDate);
- friend AFX_EXT_API CArchive& operator<<(CArchive& ar, CLDate& LDate);
- friend AFX_EXT_API CArchive& operator>>(CArchive& ar, CLDate& LDate);
-
-
- protected:
- void CheckForValidLocalDate();
- static void GetDefaultFormat();
- BOOL DST();
-
- __int64 m_nSeconds; //Actual instance data
- TimeFrame m_TimeFrame; //What Time frame does this long date represent
- BOOL m_bValid; //Is this a valid instance
-
- static BOOL sm_bDoAsserts; //Do constructor asserts
- static BOOL sm_bIsDst; //Used when determing if the date is in DST asserts in constructors / castings, otherwise sets date to invalid date
- };
-
-
-
-
-
-
-
- #endif //__DATE_H__
-
-